<?php
/**
 * Created by PhpStorm.
 * User: lizx
 * Date: 2018/2/2
 * Time: 9:15
 */

namespace app\api\controller\v1;

use app\api\controller\Fun;
use app\api\service\RegionService;
use app\common\Model\AppVersion;
use app\common\Model\BusinessUpdate;
use app\common\Model\Region;
use app\common\Server\Redis;
use think\Db;

/**
 * @title 初始化类接口V1
 * @description 接口说明
 *
 * @header name:copyright require:1 default:v1 desc:版本号(全局)
 */
class Cinit extends Base
{
    static public $validate = [];

    /**
     * @title 客户端页面初始化数据
     * @description
     * @author lizx
     * @url /api/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Cinit desc:接口组(全局)
     * @param name:command type:string require:1 default: other:initInfo desc:初始化数据
     *
     * @return data:[]
     */
    public function initInfo()
    {

//        $data = \app\common\Model\FuncClass::all(['is_del' => 0]);
        $data = \app\common\Model\FuncClass::where(['is_del' => 0])
            ->where(['status' => 1])
            ->order('sort')
            ->select();

        $list = [];
        foreach ($data as $item){
            $info = [];
            $info['func_id'] = $item->fc_id;
            $info['func_name'] = $item->func_name;
            $info['is_star'] = $item->is_star;

            $widgets = \app\common\Model\Widget::where("w_id in ({$item->widget_ids})")->select();

            $widget = [];
            foreach ($widgets as $value){
                $w = [];
                $w['widget_name'] = $value->widget_name;
                $w['is_null'] = $value->is_null;
                $w['desc'] = '';
                $w['map'] = '';

                if($value->widget_name == 'book_time'){
                    switch ($item->fc_id)
                    {
                        case 1:
                            $w['desc'] = '预约送货时间';
                            break;
                        case 2:
                            $w['desc'] = '预约取件时间';
                            break;
                        default:
                            $w['desc'] = '预约服务时间';
                    }
                }
                $widget[] = $w;
            }
            $info['widget'] = $widget;
            $list[] = $info;
        }
        return Fun::success($list);
    }

    /**
     * @title 捎货功能列表
     * @description
     * @author lizx
     * @url /api/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Cinit desc:接口组(全局)
     * @param name:command type:string require:1 default: other:funcList desc:捎货功能列表
     *
     * @return data:[]
     */
    public function funcList()
    {
        $list = \app\common\Model\FuncClass::field('fc_id as func_id,func_name,is_star')
            ->where(['is_del' => 0])
            ->where(['status' => 1])
            ->order('sort')
            ->select();
        if($list){
            $list = $list->toArray();
        }
        $item = [
            'func_id' => '0',
            'func_name' => '全部',
            'is_star' => '0',
        ];
        array_unshift($list,$item);
        return Fun::success($list);
    }

    /**
     * @title 省市区列表
     * @description
     * @author lizx
     * @url /api/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Cinit desc:接口组(全局)
     *
     * @param name:command type:string require:1 default: other:regionList desc:省市区列表
     *
     * @return data:[]
     */
    public function regionList()
    {
        //查询redis数据
        $str = 'CACHE:REGION:LIST';
        $redis = new Redis();
        $redis_region = $redis->get($str);

        //如果没有则生成数据
        if(!$redis_region){
            if(RegionService::instance()->regionList()){
                $redis_region = $redis->get($str);
            }
        }

        if($redis_region){
            return Fun::success(($redis_region));
        }
        //todo 数据为空 定义错误类型
        return Fun::error('');
    }

    /**
     * @title 接口版本号
     * @description
     * @author lizx
     * @url /api/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Cinit desc:接口组(全局)
     *
     * @param name:command type:string require:1 default: other:apiVersion desc:接口版本号
     *
     * @return data:[]
     */
    public function apiVersion()
    {
        $sql = "select type,version_no from (select `type`,version_no from sh_business_update order by version_no desc) t group by t.type";
        $list = Db::query($sql);
        //查询数据
//        $list = BusinessUpdate::field('type,version_no')->group('type')->order('version_no')->select();

        //组合数据
        $rtn = [];
        if($list){
            foreach ($list as $item){
                $rtn[$item['type']] = $item['version_no'];
            }
        }

        return Fun::success($rtn,'请求成功');
    }

    /**
     * @title APP版本号
     * @description
     * @author lizx
     * @url /api/index/index
     * @method POST
     *
     * @header name:interface require:1 default:Cinit desc:接口组(全局)
     *
     * @param name:command type:string require:1 default: other:appVersion desc:接口版本号
     * @param name:app_sys type:string require:1 default: other:appVersion desc:系统类型1安卓2iOS
     * @param name:app_type type:string require:1 default: other:appVersion desc:app类型1捎货2捎货人
     *
     * @return data:[]
     */
    public function appVersion()
    {
        $app_type = $this->arr['app_type'] ?? 0;
        $app_sys = $this->arr['app_sys'] ?? 0;

        $info = AppVersion::where(['app_type' => $app_type,'app_sys' => $app_sys])->order('version_code desc,id desc')->find();

        if(!empty($info)){
            return Fun::success($info->toArray());
        }else{
            return Fun::error('');
        }
    }
}